package engine.logic
{
	import engine.GameData;
	import engine.data.ModelLocator;
	import engine.display.newAttack.AttackLogic;
	import engine.flag.GameDefine;
	import engine.logic.login.RoleCreateUI;
	import engine.logic.tips.ItemMaxTips;
	import engine.net.datatype.WString;
	import engine.net.event.CustomMsgEvent;
	import engine.net.messages.CustomToClientFlag;
	import engine.newUi.BanksUI;
	import engine.newUi.BroadcastUI;
	import engine.newUi.ImportantMessageUI;
	import engine.newUi.MarqueeUI;
	import engine.newUi.MessageUI;
	import engine.newUi.NoticeUI;
	import engine.scene.player.RoleModel;
	import engine.ui.AlertTimeLoseUI;
	import engine.ui.MsgTipsLogic;
	import engine.ui.UIManage;
	import engine.ui.chat.ChatLogic;
	
	import flash.events.IEventDispatcher;
	
	import newx.BaseObject;
	import newx.com.config.StaticIni;
	import newx.utils.debug.IO;

	/**
	 * 响应服务端系统提示消息，调用界面类显示
	 * @author sunzhw
	 * 
	 */	
	public class AlertLogic extends BaseObject
	{
		private static		var _self				:AlertLogic;
		private 			var model				:ModelLocator;
		//根据类型建立消息堆栈
		//弹出框堆栈
		private			var _popupstack:Array	=	new Array();
		private			var	_showing:Boolean	=	false;
		private			var mq:MarqueeUI;
		
		public 				var msgTipsTime:int		= 0;
		public 				var msgTipsDelay:int	= 0;
		//需要显示的数组
		private            var _filterList:Array   = [];
//		//屏幕中上部菜单
//		private			var _topstack:Array		=	new Array();
//		//聊天框堆栈
//		private			var _chatstack:Array	=	new Array();
	
		
		static public const TIPSTYPE_SYSTEMINFO:Number		= 0;	// 系统信息(引擎模块发送的提示信息用，逻辑模块不要使用)
		static public const TIPSTYPE_SYSTEMNO:Number			= 1;		// 系统提示(客户端显示加[提示]前缀)
		static public const TIPSTYPE_ERRORINFO:Number			= 2;	// 个人出错信息
		static public const TIPSTYPE_UPGRADEINFO:Number		= 3;	// 升级信息
		static public const TIPSTYPE_ROLEBORNINFO:Number		= 4;	// 出生，复活，登陆，进入场景
		static public const TIPSTYPE_TIPSINFO:Number			= 5;		// 个人提示信息
		static public const TIPSTYPE_POPWNDINFO:Number		= 6;	// 弹出式信息(客户端游戏主界面右下角的小窗口，显示加[系统]前缀)
		static public const TIPSTYPE_POPWNDTIPS:Number		= 7;	// 弹出式信息(客户端游戏主界面右下角的小窗口，显示加[提示]前缀)
		static public const TIPSTYPE_SHOPINFO:Number			= 8;		// 摆摊相关的提示信息
		static public const TIPSTYPE_BOURSEINFO:Number		= 9;	// 寄卖相关的提示信息
		static public const TIPSTYPE_FIGHTINFO:Number			= 10;    // 战斗提示信息
		static public const TIPSTYPE_COMPETE:Number			= 11;    // 竞技场信息
		static public const TIPSTYPE_CLONE:Number				= 13;    // 副本场景信息
		static public const TIPSTYPE_FIGHT_CALC:Number		= 15;    // 战斗结算信息
		
		static public const TIPSTYPE_GM:Number				= 16;    // GM公告
		static public const TIPSTYPE_LOUDSPEAKER_NORMAL:Number= 17;    // 普通喇叭消息(消耗道具)
		static public const TIPSTYPE_LOUDSPEAKER_MASTER:Number= 18;    // 普通喇叭消息(消耗道具)
		static public const TIPSTYPE_SYSTEM_BROADCAST:Number	= 19;    // 普通喇叭消息(消耗道具)
		static public const TIPSTYPE_SYSTEM_UNENTHRALL:Number  = 20;       // 防沉迷消息
		static public const NumberTIPSTYPE_ACTIVITY_BROADCAST:Number 	= 21;// 活动广播
		static public const NumberTIPSTYPE_CHAT_BROADCAST:Number 	= 23;// 聊天框系统提示
		
		static public const TIPSYTPE_ROLEERRORINFO:Number		=22   ;		//新个人信息出错.弹界面 
//		static public const TIPSTYPE_WORD_MSG:Number			= 100;    // 城堡区域成长信息
		
		static public const TIPSTYPE_DEBUGINFO:Number			= 99;	// 调试信息
			
		
		//TOP上边提示区
		static public const TIPSTYPE_TOP_TIPSINFO:Number           = 100;		// 个人提示信息
		static public const TIPSTYPE_TOP_SYSTEMNO:Number           = 101;		// 系统提示(客户端显示加[提示]前缀)
		static public const TIPSTYPE_TOP_ERRORINFO:Number          = 102;		// 个人出错信息	
		static public const TIPSTYPE_TOP_AUTO_DISAPPEAR:Number	 = 103;		// 3秒自动消失提示
		static public const TIPSTYPE_TOP_FIGHTINFO:Number          = 104;       // 战斗提示信息
		static public const TIPSTYPE_TOP_FIGHT_CALC :Number        = 105;       // 战斗结算信息
			
		//玩家头顶的提示区
		static public const TIPSTYPE_PLAYERHEAD_TIPSINFO:Number           = 200;		// 提示信息
			
		//左下方的提示区
		static public const TIPSTYPE_LEFTBOTTOM_TIPSINFO :Number          = 300;		// 提示信息
		static public const TIPSTYPE_LEFTBOTTOM_SYSTEM_BROADCAST:Number   = 301;		 // 系统广播
		static public const TIPSTYPE_LEFTBOTTOM_ACTIVITY_BROADCAST:Number = 302;		// 活动广播
		
		//ABOVETOP特殊全服通告提示区
		static public const TIPSTYPE_ABOVETOP_TIPSINFO 		    :Number = 400;		// 特殊全服通告
		static public const TIPSTYPE_ABOVETOP_ACTIVITY_BROADCAST :Number	= 401;		// 活动广播
		static public const TIPSTYPE_ABOVETOP_HREF_NOTICE        :Number	= 402;		// 带超链接的公告
		static public const TIPSTYPE_YWINFO						:Number	= 500;		//运营广播

		
		public static function getInstance():AlertLogic
		{
			if ( _self == null )
			{
				_self = new AlertLogic();
			}
			return _self;
		}
		
		public function AlertLogic(target:IEventDispatcher=null)
		{
			super(target);
			init();
		}
		
		private function init():void
		{
			model		=	ModelLocator.getInstance();	
		
			_filterList = [	TIPSTYPE_COMPETE,
							TIPSTYPE_CLONE,
							TIPSTYPE_GM,
							TIPSTYPE_LOUDSPEAKER_NORMAL,
							TIPSTYPE_LOUDSPEAKER_MASTER,
							TIPSTYPE_SYSTEM_BROADCAST];
			
			model.addEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_HREF_NOTICE),getNotice);
			model.addEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_SEND_GUILD_RECRUIT_INFO),setFamilyNotice);
			model.addEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_IN_FIGTH_CD),setAttackNotice);
			
		}
		
		public function setAttackNotice(e:CustomMsgEvent):void
		{
			if(AttackLogic.getInstance().fighting)
				return;
			NoticeUI.getInstance().showInfo(StaticIni.getSwfLangStrVar("99295",e.pList));
		}
		
		public function setFamilyNotice(e:CustomMsgEvent):void
		{
//			var notice:String = new WString( e.pList[1]).toStringEx() 
			var notice:String =  StaticIni.getSwfLangStrVar("UIString_1000464",[new WString( e.pList[1]).toStringEx() ,"Family_" + new WString( e.pList[0]).toStringEx()]);
			ChatLogic.getInstance().setChatData({strID:"",type:NumberTIPSTYPE_ACTIVITY_BROADCAST,msg:notice});
			
//			//模拟聊天频道
//			model.dispatchEvent(new CustomMsgEvent(String(CustomToClientFlag.SERVER_CUSTOMMSG_CHAT),[GameDefine.CHATTYPE_WORLD_BY_MONEY, "", notice]));
		}
		
		private function getNotice(e:CustomMsgEvent):void
		{
			var pList : Array = e.pList;
			
			var notice 	:	String = "";
			var strLen 	:	int = pList.shift();
			var count 	:	int; 
			var strArr 	:	Array = [];
			var str		:	String;
			
			for( var i : int = 0; i < strLen; i++ )
			{
				strArr	=	[];
				str		=	pList.shift();
				count 	=	pList.shift();
				for( var j : int = 0; j < count; j++ )
				{
					//是否需要转换
					if( 0 == pList.shift() )
					{//不需要
						strArr.push( pList.shift() );
					}else
					{
						strArr.push( "notice_" + pList[ 0 ] );
						//显示ide中文
						strArr.push( StaticIni.getSwfLangStr( pList.shift() ));
					}
				}
				notice += StaticIni.getSwfLangStrVar( str, strArr );
			}
			
//			ImportantMessageUI.getInstance().showInfo( notice );
			
			ChatLogic.getInstance().setChatData({strID:"",type:NumberTIPSTYPE_ACTIVITY_BROADCAST,msg:notice});
		}
		
		public function AddAlert(type:int,strID:String,argArr:Array):void
		{
			if(!UIManage.getInstance()._uiIsCreateOver)return;
			var		msg:String		=	null;
			var 	argCount:int	=	argArr.length; 
			switch(strID)
			{	

				case "97213":
					argArr[0] =	(argArr[0] as WString).toStringEx();
					argArr[1] =	(argArr[1] as WString).toStringEx();
					argArr[2] =	targetStr(argArr[2])
					break;
				case "97215":
					argArr[0] =	StaticIni.getSwfLangStr(argArr[0]);
					argArr[1] =	(argArr[1] as WString).toStringEx()
					argArr[2] =	targetStr(argArr[2])
					break;
				case "91210":
					argArr[0] = StaticIni.getSwfLangStr("scenename_"+ argArr[0]);
					break;
				case "70006":
					argArr[0] = StaticIni.getSwfLangStr("Welcome_"+argArr[0]);
					break;
				case "80000":
					argArr[0] = StaticIni.getSwfLangStr(argArr[0]);	
					argArr[1] = StaticIni.getSwfLangStr("scenename_"+ argArr[1]);
					break;
				case "32009":
					argArr[0] = StaticIni.getSwfLangStr("AreaType_"+argArr[0]);	
					argArr[1] = StaticIni.getSwfLangStr("CastleFuncType_"+ argArr[1]);
					break;
				case "32016":
					argArr[0] = StaticIni.getSwfLangStr("CastleWork_"+argArr[0]);
					break;
				//爵位
//				case "93013":
//					var peerageObj:Object = StaticIni.getIniObj("PeerageDefine");
//					var tips:Array = (peerageObj[argArr[0]] as String).split(",");
//					argArr[0] = StaticIni.getSwfLangStr("PeerageName_" + (int(tips[0]) + 1))+ tips[2];	//1 迁就配置文件
//					break;
				case "93014":
					argArr[0] = StaticIni.getSwfLangStr("PeerageName_" + (int(argArr[0]) + 1));	//1 迁就配置文件
					break;
				case "17123":
					if(argArr[0] is WString)
					{
						IO.traceLog("AlertLogic: "+argArr[0]);
						argArr[0] = argArr[0].toStringEx();
						IO.traceLog("AlertLogic 1: "+argArr[0]);
					}
					
					var numT	:int	=	argArr[1];
					var hourT	:int	=	numT / 3600;
					var minateT	:int	=	numT % 3600 / 60;
					var secT	:int	=	numT % 3600 % 60;
					argArr[1] = hourT+StaticIni.getSwfLangStr("CastleTrain_String6")+
								minateT+StaticIni.getSwfLangStr("Common_Minute")+
								secT+StaticIni.getSwfLangStr("Common_Second");
					break;
				case "1107":
					var tempCount:String	=	"";
	
					for(var m:int = 0; m < argCount; m++)
					{  
						if(m < argCount -1)
						{
							tempCount	= tempCount.concat(StaticIni.getSwfLangStr(argArr[m])+ ", ");
						}
						else
						{
							tempCount	= tempCount.concat(StaticIni.getSwfLangStr(argArr[m]));
						}
						
					}
					argArr[0]	=	tempCount;
					break;
				case "99176":
					argArr[0]=(argArr[0] as WString).toStringEx();
					argArr[1]=StaticIni.getSwfLangStr(argArr[1]);
					break;
				
				default:
					for (var i:int = 0; i < argCount; i++)
					{
						if(argArr[i]!=null&&StaticIni.getSwfLangStr(argArr[i]).indexOf("{@")==-1)
						{
							if(argArr[i] is WString)
							{
								argArr[i] = (argArr[i] as WString).toStringEx();
							}
							else if(argArr[i] is String)
							{
								var tmpNameID:String	=	StaticIni.getIniVar(String(argArr[i]).replace(",","_"),"NameID");
								if(tmpNameID!=null&&tmpNameID!="")
								{
									argArr[i] = StaticIni.getSwfLangStr(tmpNameID);
								}else{
									argArr[i] = StaticIni.getSwfLangStr(String(argArr[i]).replace(",","_"));
								}
							}
						}
					}					
					break;					
			}
			if(strID =="99233"||strID =="99231"||strID =="99232")
			{
				argArr[2]=StaticIni.getSwfLangStr(argArr[2]);
				msg =	StaticIni.getSwfLangStrVar(strID,argArr);
			}
			else if(strID =="0" &&type ==0)  //GMCC引擎广播
			{
				msg =	argArr[0];//StaticIni.getSwfLangStrVar(strID,argArr);
			}
			else {
				msg =	StaticIni.getSwfLangStrVar(strID,argArr);
			}
			if(strID =="99244")
			{
				argArr[1]=StaticIni.getSwfLangStr(argArr[1]);
				msg =	StaticIni.getSwfLangStrVar(strID,argArr);
			}
			switch(type)
			{
	
				case TIPSTYPE_TOP_TIPSINFO:  //// 个人提示信息
				case TIPSTYPE_TOP_SYSTEMNO://Number           = 101;		// 系统提示(客户端显示加[提示]前缀)
				case TIPSTYPE_TOP_ERRORINFO://Number          = 102;		// 个人出错信息	
				
				case TIPSTYPE_TOP_FIGHTINFO://Number          = 104;       // 战斗提示信息
				case TIPSTYPE_TOP_FIGHT_CALC ://Number        = 105;       // 战斗结算信息
					MessageUI.getInstance().showInfo(msg);
//					ChatLogic.getInstance().setChatData({strID:strID,type:type,msg:msg});
//					GameData.Role.tipLogic.showInfo( msg );
					break;
				case TIPSYTPE_ROLEERRORINFO:
				case TIPSTYPE_TOP_AUTO_DISAPPEAR://3秒框
				case TIPSTYPE_TOP_AUTO_DISAPPEAR://Number	 = 103;		// 3秒自动消失提示
					NoticeUI.getInstance().showInfo(msg);
					break;
//				case TIPSTYPE_WORD_MSG:
//					TrumpetUI.generalMsg(msg);
					break;
				case TIPSTYPE_SYSTEMNO:
				case TIPSTYPE_TIPSINFO:
//					MessageUI.getInstance().showInfo(msg);
					if(GameData.Role)
					{
						var _tipLogic:MsgTipsLogic = MsgTipsLogic.getRes(GameData.Role.tipPlace);
						_tipLogic.showInfo( msg ,true);
					}
//					ChatLogic.getInstance().setChatData({strID:strID,type:type,msg:msg});
					break;
				case TIPSTYPE_ERRORINFO:
					
				case TIPSTYPE_UPGRADEINFO:
				case TIPSTYPE_ROLEBORNINFO:
//				case TIPSTYPE_FIGHTINFO:
//				case TIPSTYPE_FIGHT_CALC:
					MessageUI.getInstance().showInfo(msg);
//					ChatLogic.getInstance().setChatData({strID:strID,type:type,msg:msg});
					break;
				case TIPSTYPE_YWINFO:
				case TIPSTYPE_SYSTEMINFO:
					BroadcastUI.getInstance().showInfo(msg);
					ChatLogic.getInstance().setChatData({strID:strID,type:type,msg:msg});
					break;
				case NumberTIPSTYPE_ACTIVITY_BROADCAST:
				case TIPSTYPE_LEFTBOTTOM_ACTIVITY_BROADCAST:		// 活动广播
					ImportantMessageUI.getInstance().showInfo(msg);
					ChatLogic.getInstance().setChatData({strID:strID,type:type,msg:msg});
					break;
//				case NumberTIPSTYPE_CHAT_BROADCAST:
				case TIPSTYPE_LEFTBOTTOM_TIPSINFO:
				case TIPSTYPE_LEFTBOTTOM_SYSTEM_BROADCAST: //Number   = 301;		 // 系统广播
				case TIPSTYPE_LEFTBOTTOM_ACTIVITY_BROADCAST: //Number = 302;	
					ChatLogic.getInstance().setChatData({strID:strID,type:type,msg:msg});
//					GameData.Role.tipLogic.showInfo( msg,true );
					break;
				case TIPSTYPE_PLAYERHEAD_TIPSINFO://玩家头顶
					if(GameData.Role)
					{
						MsgTipsLogic.getRes(GameData.Role.tipPlace).showInfo(msg,true);						
					}
//					GameData.Role.tipLogic.showInfo( msg,true );
					break;
				case TIPSTYPE_ABOVETOP_TIPSINFO:
				case  TIPSTYPE_ABOVETOP_ACTIVITY_BROADCAST:
				case TIPSTYPE_ABOVETOP_HREF_NOTICE:
					ImportantMessageUI.getInstance().showInfo(msg);
					break;
				case TIPSTYPE_CLONE:
					
			//		InstanceMapUI.getInstance().setCopyMapLog(msg);	
				case TIPSTYPE_SYSTEM_UNENTHRALL:
					AlertTimeLoseUI.getInstance().showTimeLoseUI(strID, type, msg);
				default:	
					msgFilter(type,msg);
//					ChatLogic.getInstance().setChatData({strID:strID,type:type,msg:msg});
					break;
			}	
		}
		/**
		 * 特殊处理开启宝箱得到高级物品， 
		 * @return 
		 * 
		 */		
		private function targetStr(val:String):String
		{
			var itemObj:Object		=	StaticIni.getIniObj(val);
			//var configID:String = itemObj["NameID"];
			var bindStatus:int		= 	itemObj["BindStatus"];
			var bindType:int 		= 	itemObj["BindType"];
			var holeCount:int 		= 	itemObj["HoleCount"];
			var intensityLevel:int	=	itemObj["IntensityLevel"];
			var intensityType:int	=	itemObj["IntensityType"];
			//附加属性
			var addProps : String 	= 	"";
			var len		:int	=	int(StaticIni.getIniVar("ProTipList","addPropNum"));
			for(var i : int = 0; i < len; i++){
				if(itemObj["AddPropNo"+i] == null)
				{
					itemObj["AddPropNo"+i]	=	0;
				}
				addProps +=itemObj["AddPropNo"+i] + ";";
			}
			
			//装备名
			var itemName :String	= 	StaticIni.getIniVar(val,"NameID");
			var color :int 			= 	int(StaticIni.getIniVar(val,"Color"));
			itemName 				= 	StaticIni.getSwfLangStr(itemName);
			itemName 				= 	link_color(itemName,color);
			var itemUrl:String = "<a href=\"event:item_"+ "," +val + "," + bindType + "," 
				+ bindStatus + "," + addProps  +","+holeCount+","+intensityLevel+ ","+intensityType+"\">"
				+ StaticIni.getSwfLangStrVar("UIStr_1000587",[itemName])+"</a>";
			return itemUrl;
		}
		private function link_color(str:String,color:int):String
		{
			if(color < 1 || color > 6)
			{
				color	=	1;
			}
			return ("<font color ='"+ItemMaxTips.COLOR_TABLE[color]+str+"</font>");
		}
		/**
		 * 消息过滤
		 * @param $msgType 消息类型
		 * @param $msg     消息内容
		 */
		private function msgFilter($msgType:int,$msg:String):void
		{
			if(_filterList.indexOf($msgType) != -1){
				_popupstack.push($msg);
				ShowMsg();
			}
		}
		
		public function ShowMsg():void
		{
			if(_showing)
			{
				return ;
			}
			var	title:String	=	_popupstack.shift();
		//	IO.traceLog("===============title msg:::::::::::::"+title);
//			MarqueeUI.generalNews(title);
//			TrumpetUI.generalMsg(title);
		}

		public function get showing():Boolean
		{
			return _showing;
		}

		public function set showing(value:Boolean):void
		{
			_showing = value;
		}
	}
}